<template>
  <h2>智能导诊程序</h2>
  <div class="container">
    <p>您好，请描述您的症状，我们将为您推荐就诊科室:</p>
    <el-input
      v-model="textarea"
      :autosize="{ minRows: 2, maxRows: 4 }"
      type="textarea"
      placeholder="输入症状"
      @keyup.enter="submitForm"
    />
    <div class="btns">
      <el-button type="primary" @click="submitForm"> 确认 </el-button>
      <el-button type="warning" @click="goUrl('zhishiku')"> 修改知识库 </el-button>
      <el-button type="info" @click="goUrl('home')"> 返回首页 </el-button>
    </div>
    <div class="result" v-if="answer">
      <p>结果如下:</p>
      <template v-if="results.length < 1">
        <p>很抱歉，您可能需要呼叫人工服务。</p>
      </template>
      <template v-else>
        <p v-for="(item, index) in results" :key="index">
          推理[{{ index + 1 }}]：{{ item[0] }}-->{{ item[1] }}
        </p>
      </template>
    </div>
    <div class="result" v-else>
      <p>点击确认，获取推理结果</p>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import zhishikuData from "@/assets/data.js";
const router = useRouter();

const dataBase = ref("");
const textarea = ref("");
const results = ref([]);
const answer = ref(false);

onMounted(() => {
  let zsk = localStorage.getItem("zhishiku");
  if (zsk === null) {
    let zhishiku = parseRules(zhishikuData); // 将文件内容转为字符串
    localStorage.setItem("zhishiku", JSON.stringify(zhishiku));
    dataBase.value = zhishiku;
  } else {
    dataBase.value = JSON.parse(zsk);
  }
});

const parseRules = (input) => {
  // 初始化一个空对象用于存储结果
  const result = {};

  // 按行分割字符串
  const lines = input.split("\n");

  // 遍历每一行
  lines.forEach((line) => {
    // 去除首尾空格
    line = line.trim();

    // 跳过空行
    if (!line) return;

    // 检查是否符合 "如果 条件 那么 结果" 的格式
    const match = line.match(/^如果\s+(.+?)\s+那么\s+(.+)$/);
    if (match) {
      const condition = match[1].trim(); // 提取条件
      const consequence = match[2].trim(); // 提取结果
      result[condition] = consequence; // 存入结果对象
    }
  });

  return result;
};
const submitForm = () => {
  answer.value = false;
  results.value = [];
  ElMessage.closeAll();
  if (textarea.value === "") {
    ElMessage.warning("请描述您的症状。");
  } else {
    let keys = Object.keys(dataBase.value);
    let res = keys.filter((k) => {
      return textarea.value.includes(k);
    });
    res.forEach((r) => {
      addTl(r);
    });

    answer.value = true;
  }
};
const addTl = (key) => {
  let keys = Object.keys(dataBase.value);
  results.value.push([key, dataBase.value[key]]);
  if (keys.includes(dataBase.value[key])) {
    addTl(dataBase.value[key]);
  }
};
const goUrl = (name) => {
  router.push({ name: name });
};
</script>

<style lang="scss" scoped>
.container {
  text-align: left;
  width: 50vw;
  min-width: 300px;
}
.btns {
  margin-top: 30px;display: flex;
  align-items: center;
  justify-content: space-around;
}
.result {
  margin-top: 30px;
  p {
    padding: 0;
    margin: 0 0 10px;
  }
  display: flex;
  flex-direction: column;
  color: red;
}
</style>
